<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title"
      :width="modalWidth"
      @cancel="handleClose"
      @ok="handleOk"
      :destroyOnClose="true"
      :visible="visible"
      :confirmLoading="confirmLoading"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="modelForm" :model="model" :rules="rules">
          <a-form-model-item
            label="菜单类型"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <a-radio-group
              :disabled="isDisabled"
              @change="onChangeMenuType"
              v-model.trim="model.type"
              :options="menuOption"
            >
            </a-radio-group>
          </a-form-model-item>

          <a-form-model-item
            v-if="model.type !== '0'"
            :label="caseParent(model.type)"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            :validate-status="validateStatus"
            :hasFeedback="true"
          >
            <a-tree-select
              v-model.trim="model.parentId"
              style="width: 100%"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              :tree-data="treeData"
              :replaceFields="replaceFields"
              tree-default-expand-all
            ></a-tree-select>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            :label="caseName(model.type)"
            prop="name"
            hasFeedback
          >
            <a-input v-model.trim="model.name" />
          </a-form-model-item>

          <a-form-model-item
            v-if="model.type === '2'"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="pathName"
            label="组件名称"
          >
            <a-input
              placeholder="请输入组件名称"
              v-model.trim="model.pathName"
            />
          </a-form-model-item>

          <a-form-model-item
            v-if="model.type === '0' || model.type === '2'"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            :label="casePath(model.type)"
            prop="path"
            hasFeedback
          >
            <a-input v-model.trim="model.path" />
          </a-form-model-item>

          <a-form-model-item
            v-if="model.type === '0'"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="app标识"
            hasFeedback
          >
            <a-input v-model.trim="model.appFlag" />
          </a-form-model-item>

          <a-form-model-item
            v-if="model.type !== '0'"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="icon标识"
            hasFeedback
          >
            <a-input placeholder="点击选择图标" v-model.trim="model.iconFlag">
              <a-icon slot="addonAfter" type="setting" @click="selectIcons" />
            </a-input>
          </a-form-model-item>

          <a-form-model-item
            v-if="model.type == '2'"
            label="是否显示菜单"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <a-radio-group v-model.trim="model.ifShow" :options="showOption">
            </a-radio-group>
          </a-form-model-item>

          <a-form-model-item
            v-if="model.type == '2' || model.type == '1'"
            label="缓存组件"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <a-radio-group
              v-model.trim="model.refresh"
              :options="refreshOption"
            >
            </a-radio-group>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="排序"
            prop="sort"
            hasFeedback
          >
            <a-input placeholder="请输入排序" v-model.trim="model.sort" />
          </a-form-model-item>
        </a-form-model>
        <!-- 选择图标 -->
        <icons
          @choose="handleIconChoose"
          @close="handleIconCancel"
          :iconChooseVisible="iconChooseVisible"
        ></icons>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import api from "@/api/apiServer";
import Icons from "@/components/Icon/index.vue";
export default {
  props: {
    treeData: {
      type: Array,
    },
    selectedRowKeys: {
      type: Array,
    },
  },
  components: { Icons },
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 652, // 抽屉宽度
      isDisabled: false,
      model: {
        type: "0",
        refresh: 0,
      },
      replaceFields: {
        title: "name",
        key: "id",
        value: "id",
        children: "childList",
      },

      menuOption: [
        { label: "app", value: "0" },
        { label: "模块", value: "1" },
        { label: "页面", value: "2" },
        { label: "按钮", value: "3" },
      ],
      showOption: [
        { label: "是", value: "1" },
        { label: "否", value: "0" },
      ],
      refreshOption: [
        { label: "是", value: 1 },
        { label: "否", value: 0 },
      ],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      iconChooseVisible: false, // 图标选择组件
      validateStatus: "",
    };
  },
  computed: {
    rules() {
      return {
        // name: [{ required: true, message: "请输入菜单标题!" }],
      };
    },
  },
  mounted() {},
  methods: {
    // 新增
    modalAdd() {
      this.modalEdit(this.model);
    },
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      if (record.id) {
        this.isDisabled = true;
      }
      this.visible = true;
      this.model = Object.assign({}, record);
      if (this.model.refresh == null) {
        this.$set(this.model, "refresh", 0);
      }
    },

    async submitVulFun() {
      this.confirmLoading = true;
      if (this.model.type === "0") {
        this.model.parentId = 0;
      }
      let res;
      if (!this.model.id) {
        res = await api.ResourcesAdd(this.model);
      } else {
        res = await api.ResourcesEdit(this.model);
      }
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        return;
      }
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$emit("ok", this.selectedRowKeys);
      this.visible = false;
      this.model = Object.assign({});
      this.isDisabled = false;
      this.$set(this.model, "type", "0");
    },
    onChangeMenuType(e) {
      this.model = Object.assign({});
      this.$set(this.model, "ifShow", "1");
      this.$set(this.model, "type", e.target.value);
    },
    // 图标选择
    selectIcons() {
      this.iconChooseVisible = true;
    },
    handleIconCancel() {
      this.iconChooseVisible = false;
    },
    handleIconChoose(value) {
      this.model.iconFlag = value;
      this.iconChooseVisible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.modelForm.validate((valid) => {
        if (valid) {
          that.submitVulFun();
        } else {
          return false;
        }
      });
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.isDisabled = false;
      this.model = Object.assign({});
      this.$set(this.model, "type", "0");
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 652;
      }
    },

    caseName(type) {
      switch (type) {
        case "0":
          return "应用名称";
        case "1":
          return "模块名称";
        case "2":
          return "页面名称";
        case "3":
          return "按钮名称";
      }
    },
    caseParent(type) {
      switch (type) {
        case "1":
          return "app节点";
        case "2":
          return "模块节点";
        case "3":
          return "页面节点";
      }
    },
    casePath(type) {
      switch (type) {
        case "0":
          return "应用地址";
        case "1":
          return "模块地址";
        case "2":
          return "页面路由";
        case "3":
          return "按钮地址";
      }
    },
  },
};
</script>


